home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / itrns211.zip / SRC / DEANSIFY.C next >
C/C++ Source or Header  |  1991-08-25  |  4KB  |  154 lines

  1. /* ABC: 5/9/91: I did not write this program (de-ansify).
  2.  * I found it lying around, unfortunately, I do not know who originally
  3.  * wrote it, so am leaving it unattributed....
  4.  * (This IS public domain code --- most probably from dvips or xgl.)
  5.  * [abc] modified to handle the %{ starting brace of ilex.l and iyacc.y
  6.  ***************************************************************************
  7.  */
  8.  
  9. /* This doesn't handle prototype args within the declaration for a pointer
  10.    to a function type. */
  11. #include <ctype.h>
  12. #define ARGBUFSIZE 1024
  13. #define my_isalnum(c) ((c=='_') || isalnum(c))
  14. #define my_isalpha(c) ((c=='_') || isalpha(c))
  15.  
  16. int c;
  17. main() {
  18. int possible=0, braces=0, gotslash=0, gotpercent=0, gotstar=0, gotnl=0;
  19.     for (c=getchar(); c != -1;) {
  20.     if (gotslash && (c=='*')) {
  21.         putchar(c);
  22.         c=getchar();
  23.         do {
  24.         putchar(c);
  25.         gotstar = (c == '*');
  26.         } while (((c=getchar()) != -1) && (!gotstar || (c != '/')));
  27.         continue;
  28.     } else if (gotnl && (c=='#')) {
  29.         do {
  30.         putchar(c);
  31.         gotslash=(c=='\\');
  32.         } while (((c=getchar()) != -1) && (gotslash || (c!='\n')));
  33.     }
  34.     /* abc new code */
  35.     if (!gotpercent && c=='{') {
  36.         braces++;
  37.     } else if (!gotpercent && c=='}') {
  38.         braces--;
  39.     }
  40.     gotpercent = (c=='%');
  41.     /* abc end new code */
  42.     gotslash = (c=='/');
  43.     gotnl = (c=='\n');
  44.  
  45. #ifdef OLD_DEAN_CODE
  46.     if (c=='{') {
  47.         braces++;
  48.     } else if (c=='}') {
  49.         braces--;
  50.     } else
  51. #endif /*OLD_DEAN_CODE*/
  52.        if (!braces) {
  53.         if (c==';') {
  54.         possible=0;
  55.         } else if (c=='(') {
  56.         if (possible) {
  57.             putchar(c);
  58.             c=getchar();
  59.             convert_arglist();
  60.             possible=0;
  61.             continue;
  62.         }
  63.         } else if (my_isalpha(c)) {
  64.         possible=1;
  65.         putchar(c);
  66.         while (((c=getchar()) != -1) && my_isalnum(c))
  67.             putchar(c);
  68.         continue;
  69.         }
  70.     }
  71.     putchar(c);
  72.     c=getchar();
  73.     }
  74. }
  75.  
  76.  
  77. convert_arglist()
  78. {
  79. char argbuf[ARGBUFSIZE],argc;
  80. char typebuf[ARGBUFSIZE];
  81. char protobuf[ARGBUFSIZE];
  82. char lastword[ARGBUFSIZE];
  83. int parens=1,len=0,pi,ti,ai,li;
  84. int wordc;
  85.     lastword[0]=0;
  86.     typebuf[0]=0;
  87.     while((c!=-1) && parens) {
  88.     if (c=='(')
  89.         parens++;
  90.     if (c==')')
  91.         parens--;
  92.     protobuf[len++]=c;
  93.     c = getchar();
  94.     }
  95.     while ((c != -1) && isspace(c)) {
  96.     protobuf[len++]=c;
  97.     c = getchar();
  98.     }
  99.     protobuf[len]=0;
  100.     if (c == ';') {
  101.     printf(");");
  102.     c = getchar();
  103.     return;
  104.     }
  105.     for (wordc=pi=ti=ai=li=0,parens=1; (pi<len) && parens;) {
  106.     if ((parens==1) && (protobuf[pi]==',')) {
  107.         sprintf(argbuf+ai,"%s, ",lastword);
  108.         ai=strlen(argbuf);
  109.         typebuf[ti++]=';';
  110.         typebuf[ti++]='\n';
  111.         pi++;
  112.         if (wordc==1) {
  113.         printf("%s",protobuf);
  114.         return;
  115.         }
  116.         wordc = 0;
  117.     } else {
  118.         if (protobuf[pi]=='(') parens++;
  119.         if (protobuf[pi]==')') parens--;
  120.         if (parens==0) {
  121.         if (ti >0) {
  122.             typebuf[ti++]=';';
  123.             typebuf[ti++]='\n';
  124.         }
  125.         pi++;
  126.         break;
  127.         }
  128.         typebuf[ti++]=protobuf[pi];
  129.         li=0;
  130.         if (my_isalpha(protobuf[pi])) {
  131.         lastword[li++]=protobuf[pi++];
  132.         wordc++;
  133.         while ((pi<len) && my_isalnum(protobuf[pi])) {
  134.             typebuf[ti++]=protobuf[pi];
  135.             lastword[li++]=protobuf[pi];
  136.             pi++;
  137.         }
  138.         lastword[li]=0;
  139.         } else
  140.         pi++;
  141.     }
  142.     }
  143.     if (wordc<=1) {
  144.     printf("%s",protobuf);
  145.     return;
  146.     }
  147.     typebuf[ti]=0;
  148.     sprintf(argbuf+ai,"%s)\n",lastword);
  149.     printf("%s",argbuf);
  150.     printf("%s",typebuf);
  151.     if (protobuf[pi])
  152.     printf("%s\n",protobuf+pi);
  153. }
  154.